package com.buptse.service;

import com.buptse.dao.CityMapper;
import com.buptse.dao.EarthQuakeMapMapper;
import com.buptse.dao.InformationMapper;
import com.buptse.model.EarthQuakeMap;
import com.buptse.model.Information;
import com.buptse.model.InformationExample;
import com.buptse.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.concurrent.ThreadLocalRandom;

@Service
public class InformationService {
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private EarthQuakeMapMapper earthQuakeMapMapper;

    //地震地图
    public List<Map<String,Object>> getEarthQuakeMap(){
        List<Map<String,Object>> earthQuakeMapList=new ArrayList<>();
        List<EarthQuakeMap> list=earthQuakeMapMapper.selectByExample(null);
        for(EarthQuakeMap earthQuakeMap:list){
            Map<String,Object> map=new HashMap<>();
            map.put("adcode",earthQuakeMap.getAdcode());
            map.put("earthQuakeLevel",Integer.parseInt(earthQuakeMap.getEarthquakelevel()));
            earthQuakeMapList.add(map);
        }
        return earthQuakeMapList;
    }

    //解析图片、视频、音频
    public void photoAndVideoDecode(String code,String description) throws ParseException {
        Information information=decode(code);
        information.setDescription(description);
        informationMapper.insert(information);
    }

    //解析excel
    public void excelDecode(String code,String description) throws ParseException {
        Information information=decode(code);
        information.setDescription(description);
        informationMapper.insert(information);
    }

    //解码
    public Information decode(String code) throws ParseException {
        Information information=new Information();
        //地震信息的编码
        information.setCode(code);
        //随机数
        ThreadLocalRandom threadLocalRandom=ThreadLocalRandom.current();
        //前6位地区码
        String areaCode=code.substring(0,6);
        //地震信息的地址
        Integer earthquakeLevel=new Integer(threadLocalRandom.nextInt(Integer.parseInt(areaCode)%8+1));
        earthQuakeMapMapper.insert(new EarthQuakeMap(areaCode,earthquakeLevel.toString()));
        areaCode=code.substring(0,12);
        LocationDecoder.locationDecode(areaCode,cityMapper,information);
        //地震信息的时间
        String date=code.substring(12,26);
        StringBuffer stringBuffer=new StringBuffer(date);
        stringBuffer.insert(4,"-");
        stringBuffer.insert(7,"-");
        stringBuffer.insert(10," ");
        stringBuffer.insert(13,":");
        stringBuffer.insert(16,":");
        date=stringBuffer.toString();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        information.setDating(simpleDateFormat.parse(date));
        //地震信息的来源
        String source=code.substring(26,29);
        SourceDecoder.sourceDecode(source,information);
        //地震信息的载体
        String carrier= String.valueOf(code.charAt(29));
        CarrierDecoder.carrierDecode(carrier,information);
        //地震信息的种类和指标
        String parentCategory= String.valueOf(code.charAt(30));
        String directory=parentCategory+code.substring(31,33);
        DirectoryDecoder.directoryDecode(directory,information);
        String index=parentCategory+code.substring(33,36);
        IndexDecoder.indexDecode(index,information);
        return information;
    }

    //获取所有地震信息
    public List<Information> getEarthquakeInformation(){
        List<Information> informationList=informationMapper.selectByExample(null);
        return informationList;
    }

    //获取各省地震次数
    public List<Map<String,Object>> getProvinceEarthquakeCount(){
        Map<String,Integer> provinceEarthquakeMap=new HashMap<>();
        List<Information> informationList=informationMapper.selectByExample(null);
        for(Information information:informationList){
            int index=information.getLocation().indexOf(" ");
            if(index<0) continue;
            String province=information.getLocation().substring(0,index);
            Integer provinceEarthquakeCount=provinceEarthquakeMap.get(province);
            if(provinceEarthquakeCount==null){
                provinceEarthquakeMap.put(province,0);
            }else{
                provinceEarthquakeMap.put(province,provinceEarthquakeCount+1);
            }
        }

        List<Map<String,Object>> provinceEarthquakeMapList=new ArrayList<>();
        for(Map.Entry<String, Integer> map:provinceEarthquakeMap.entrySet()){
            Map<String,Object> provinceMap=new HashMap<>();
            provinceMap.put("province",map.getKey());
            provinceMap.put("count",map.getValue());
            provinceEarthquakeMapList.add(provinceMap);
        }

        return provinceEarthquakeMapList;
    }

    //获得每月的地震信息
    public List<Map<String,Object>> getMonthlyCount(){
        List<Information> informationList=getAllData();
        Map<String,Integer> monthlyStats=new HashMap<>();
        for(Information information:informationList){
            String month=(information.getDating().getYear()+1900)+"-"+information.getDating().getMonth();
            if(monthlyStats.containsKey(month)){
                monthlyStats.put(month,monthlyStats.get(month)+1);
            } else {
                monthlyStats.put(month,1);
            }
        }
        List<Map<String,Object>> monthlyEarthquakeMapList=new ArrayList<>();
        for(Map.Entry<String, Integer> map:monthlyStats.entrySet()){
            Map<String,Object> provinceMap=new HashMap<>();
            provinceMap.put("year",map.getKey());
            provinceMap.put("count",map.getValue());
            monthlyEarthquakeMapList.add(provinceMap);
        }
        return monthlyEarthquakeMapList;
    }

    //数据删除
    public List<Information> deleteData(int id){
        InformationExample informationExample=new InformationExample();
        informationExample.createCriteria().andIdEqualTo(id);
        informationMapper.deleteByExample(informationExample);
        return getAllData();
    }

    //获取当前数据
    //可能要根据不同的条件example筛选不同数据
    public List<Information> getAllData(){
        return informationMapper.selectByExample(null);
    }


    /**
     *根据输入插入灾情信息
     */
    public void insertData(Information information) throws ParseException {
        informationMapper.insert(information);
    }
}
